package com.zzyl.nursing.controller.member;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.zzyl.common.core.domain.R;
import com.zzyl.nursing.dto.DeviceDataQueryDTO;
import com.zzyl.nursing.domain.member.DevicePropertyData;
import com.zzyl.nursing.domain.member.FamilyElderData;
import com.zzyl.nursing.domain.member.FamilyMemberElder;
import com.zzyl.nursing.domain.member.FamilyMemberElderMy;
import com.zzyl.nursing.dto.FamilyDto;
import com.zzyl.nursing.dto.UserLoginRequestDto;
import com.zzyl.nursing.service.IDeviceService;
import com.zzyl.nursing.vo.DeviceDataByDay;
import com.zzyl.nursing.vo.DeviceShadowDataVo;
import com.zzyl.nursing.service.IFamilyMemberElderService;
import com.zzyl.nursing.vo.LoginVo;
import io.swagger.annotations.*;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zzyl.common.annotation.Log;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.nursing.domain.FamilyMember;
import com.zzyl.nursing.service.IFamilyMemberService;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.common.core.page.TableDataInfo;

/**
 * 老人家属Controller
 * 
 * @author laoYe
 * @date 2025-05-29
 */
@RestController
@RequestMapping("/member/user")
@Api(tags = "老人家属相关接口")
public class FamilyMemberController extends BaseController
{
    @Autowired
    private IFamilyMemberService familyMemberService;
    @Autowired
    private IFamilyMemberElderService familyMemberElderService;


    /**
     * 小程序端登录
     * @param dto
     * @return
     */
    @PostMapping("/login")
    public R<LoginVo> login(@RequestBody UserLoginRequestDto dto){
        LoginVo vo = familyMemberService.login(dto);
        return R.ok(vo);
    }

//    /**
//     * 1.4 查询健康数据
//     * @param
//     * @return
//     */
//    @PostMapping("/queryServiceProperties/{iotId}")
//    @ApiOperation("查询健康数据")
//    //ApiImplicitParams描述参数类型为对象，有哪些属性是真正的参数
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "iotId", value = "物联网设备id", required = true, dataTypeClass = String.class)
//    })
//    public R<List<DeviceShadowDataVo>> queryServiceProperties(@PathVariable("iotId") String iotId) {
//        List<DeviceShadowDataVo> voList = familyMemberService.queryServiceProperties(iotId);
//        return R.ok(voList);
//    }


    /**
     * 1.4 按天统计查询指标数据
     * @param
     * @return
     */
    @GetMapping("/queryDeviceDataListByDay")
    @ApiOperation("按天统计查询指标数据")
    public R<List<DeviceDataByDay>> queryDeviceDataListByDay( DeviceDataQueryDTO dto) {
        List<DeviceDataByDay> dataList = familyMemberService.queryDeviceDataListByDay(
                dto.getIotId(), dto.getFunctionId(), dto.getStartTime(), dto.getEndTime());
        return R.ok(dataList);
    }
    /**
     * 1.4 按周统计查询指标数据
     * @param
     * @return
     */
    @GetMapping("/queryDeviceDataListByWeek")
    @ApiOperation("按天统计查询指标数据")
    public R<List<DeviceDataByDay>> queryDeviceDataListByWeek( DeviceDataQueryDTO dto) {
        List<DeviceDataByDay> dataList = familyMemberService.queryDeviceDataListByWeek(
                dto.getIotId(), dto.getFunctionId(), dto.getStartTime(), dto.getEndTime());
        return R.ok(dataList);
    }

    /**
     * 绑定家人
     */
    @PostMapping("/add")
    public R<String> add(@RequestBody FamilyDto family){
        //先用Dto获取老人的id 然后放入FamilyMemberElder里面 添加他们的关系
        FamilyMemberElder familyMemberElder = familyMemberService.selectFamilyMemberId(family);
        familyMemberElderService.insertFamilyMemberElder(familyMemberElder);
        return R.ok("绑定成功");
    }

    /**
     * 查询家人列表
     */
    @GetMapping("/my")
    public R<List<FamilyMemberElderMy>> my() {
        List<FamilyMemberElderMy> list = familyMemberService.selectFamilyMemberElderMy();

        return R.ok(list);
    }

    /**
     * 查询已绑定的家人列表 分页查询
     */
    @GetMapping("/list-by-page")
    public R<List<FamilyElderData>> listByPage(Integer pageNum, Integer pageSize){
        startPage();
        List<FamilyElderData> list = familyMemberService.selectFamilyElderData();
        return R.ok(list);
    }

    /**
     * 获取绑定设备的数据
     */
    @GetMapping("/queryServiceProperties/{deviceId}")
    public R<List<DevicePropertyData>> queryServiceProperties(@PathVariable String deviceId){
        List<DevicePropertyData> list  = familyMemberService.getDeviceData(deviceId);
        return R.ok(list);
    }

}
